﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;

namespace GE_Management_Tool.Planning
{
    public partial class frmA0012_Project_Group : frmBase
    {
        IList<CProject_Group> m_lsProject_Group;
        CProject_Group m_objSelected;

        public frmA0012_Project_Group()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
            this.grvProjectBatch.OptionsBehavior.Editable = true;
        }

        private void Init_Data()
        {
            m_lsProject_Group = new List<CProject_Group>();
            CProject_Group_Controller v_ctrlGroup = new CProject_Group_Controller();
            m_lsProject_Group = v_ctrlGroup.A001_List_Project_Group_By_Project_ID(CConfig.g_intGem_Tools_Current_Project);
            cProjectGroupBindingSource.DataSource = m_lsProject_Group;
        }

        private void frmA0012_Project_Group_Load(object sender, EventArgs e)
        {
            try
            {
                Init_Data();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private string Check_Data()
        {
            StringBuilder v_Sb = new StringBuilder();
            if (m_objSelected.Group_Name == "")
            {
                v_Sb.AppendLine("Missing Batch Name.");
            }

            return v_Sb.ToString();
        }

        private void grvProjectBatch_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                m_objSelected.Project_ID = CConfig.g_intGem_Tools_Current_Project;
                string error = Check_Data();
                if (error != "")
                {
                    throw new Exception(error);
                }
                //lúc này là mới thêm vào cho nên Auto ID  = 0;
                if (m_objSelected.Auto_ID <= 0)
                {
                    CProject_Group_Controller v_ctrlGroup = new CProject_Group_Controller();
                    m_objSelected.Auto_ID = v_ctrlGroup.Insert_Project_Group(m_objSelected);
                    XtraMessageBox.Show("Insert Success !");
                }
                else
                {
                    // ở đây là thêm vào hệ thống tự sinh ra auto_id rồi nên auto_id chắc chắn > 0;
                    CProject_Group_Controller v_ctrlGroup = new CProject_Group_Controller();
                    v_ctrlGroup.Update_Project_Group(m_objSelected);
                    XtraMessageBox.Show("Update Success !");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grdProjectBatch_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                if (XtraMessageBox.Show("Are you sure you want to delete this row ?", "Warning", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    CProject_Group_Controller v_ctrlGroup = new CProject_Group_Controller();
                    v_ctrlGroup.Delete_Project_Group(m_objSelected.Auto_ID, CSession.Active_User_Name);
                }
            }
        }

        private void grvProjectBatch_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objSelected = (CProject_Group)grvProjectBatch.GetRow(grvProjectBatch.FocusedRowHandle);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }
    }
}